//
//  YSAppDelegate.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-8-28.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSAppDelegate.h"
#import "YSRoomListViewController.h"
#import "JSONKit.h"
#import "YSDefine.h"
#import "YSGlobal.h"
#import "YSUtility.h"
#import "UINavigationController+Autorotate.h"

@implementation YSAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self start];
    [application setIdleTimerDisabled:YES];
    [WXApi registerApp:WX_App_Id];
    
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:WB_App_Key];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    YSRoomListViewController *roomList = [[YSRoomListViewController alloc] initWithNibName:@"YSRoomListViewController" bundle:nil];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:roomList];
    nav.delegate = roomList;
    
    self.window.rootViewController = nav;
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
}

- (void)applicationWillTerminate:(UIApplication *)application
{
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    if ([url.scheme isEqualToString:@"wx0efabc4ae60261b1"]) {
        return [WXApi handleOpenURL:url delegate:self];
    }
    return [WeiboSDK handleOpenURL:url delegate:self];
}

+ (YSAppDelegate *)sharedDelegate
{
    return (YSAppDelegate*)[[UIApplication sharedApplication] delegate];
}

#pragma mark - NetWorkMgrDelegate
- (void)start {
    [[NetworkMgr sharedManager] asynchRequestWithFunId:eAPI_START
                                              delegate:self
                                                params:nil];
}


- (void)didSuccess:(NetworkMgr *)api userInfo:(NSDictionary *)info {

    NSInteger funId = [info[kFUN_ID] integerValue];
    NSDictionary *re = info[API_Result_RE];
    if ([YSUtility isBlankObject:re]) {
        return;
    }
    
    re = [YSUtility returnDictionary:re];
    
    switch (funId) {
        case eAPI_START: {
            NSString *sysTime = re[@"sysTime"];
            if (![sysTime isKindOfClass:[NSString class]]) {
                NSNumber *numb = [NSNumber numberWithLongLong:[sysTime longLongValue]];
                sysTime = [numb stringValue];
            }
            [YSGlobal setSysTime:[sysTime floatValue]];
            [YSGlobal setUID:re[@"uid"]];
            
            BOOL isFirstLogin = [re[@"isFirstLogin"] boolValue];
            
            if (isFirstLogin) {
                [self performSelector:@selector(firstLogin)
                           withObject:nil
                           afterDelay:1];
            }
        }
            break;
            
        default:
            break;
    }
}

- (void) didFail:(NetworkMgr *)api userInfo:(NSDictionary *)info {
    NSLog(@"fail  %@",info);
}

- (void)firstLogin {
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationFirstLogin
                                                        object:nil
                                                      userInfo:nil];
}

#pragma mark - WX-delegate
- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp *)resp;
        
        switch (response.errCode) {
            case WXSuccess:
                NSLog(@"wx pay success");
                // kNotificationWeiXinPaySuccess
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWeiXinPaySuccess
                                                                    object:nil];
                break;
            default:
                NSLog(@"wx pay fail");
                // kNotificationWeiXinPayFail
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWeiXinPayFail
                                                                    object:nil];
                break;
        }
    } else if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        SendMessageToWXResp *response = (SendMessageToWXResp *)resp;
        switch (response.errCode) {
            case WXSuccess:
                NSLog(@"share wei xin success");
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWeiXinShareSuccess
                                                                    object:nil];
                break;
            default:
                NSLog(@"share wei xin fail");
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWeiXinShareFail
                                                                    object:nil];
                break;
        }
    }
}

#pragma mark - WeiBo Delegate
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
    if (response.statusCode == WeiboSDKResponseStatusCodeSuccess) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWeiBoShareSuccess
                                                            object:nil];
    } else if (response.statusCode != WeiboSDKResponseStatusCodeUserCancel &&
               response.statusCode != WeiboSDKResponseStatusCodeUserCancelInstall) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWeiBoShareFail
                                                            object:nil];
    }
}

@end
